﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text.RegularExpressions;
using Microsoft.Phone.Tasks;

namespace DevCorner.Weather.WP7.Pages
{
  public partial class SaveEmail : PhoneApplicationPage
  {
    //Regex patterns provided by http://regexlib.com/
    const string emailAddressPattern = @"^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$";
    SaveEmailAddressTask saveEmailAddressTask;

    public SaveEmail()
    {
      InitializeComponent();

      saveEmailAddressTask = new SaveEmailAddressTask();
      saveEmailAddressTask.Completed += (s, args) =>
      {
        if (args.TaskResult == TaskResult.OK)
          MessageBox.Show("Email address saved successfully", "Success", MessageBoxButton.OK);
        else
          MessageBox.Show("Error while saving phone number:\n" + args.Error.Message, "Fail", MessageBoxButton.OK);

      };

    }

    private void btnSaveEmail_Click(object sender, RoutedEventArgs e)
    {
      if (Regex.IsMatch(txtEMail.Text, emailAddressPattern, RegexOptions.IgnoreCase))
      {
        SaveEmailAddressTask saveEmailAddressTask = new SaveEmailAddressTask();
        saveEmailAddressTask.Email = txtEMail.Text;
        saveEmailAddressTask.Show();
        txtEMail.Text = "";
      }
      else
        MessageBox.Show("Specified value is not a email address", "Invalid Input", MessageBoxButton.OK);

    }
  }
}